package cn.dengta.context.model;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import cn.dengta.common.model.ValueProxyImpl;
import me.codeplayer.util.StringUtil;

public interface OptionProxyImpl<E extends Enum<E>, V> extends ValueProxyImpl<E, V> {

	static List<Option<String>> toMetas(LabelI18nProxy<?, ?>[] values) {
		return toMetas(values, (Language) null);
	}

	static List<Option<String>> toMetas(LabelI18nProxy<?, ?>[] values, @Nullable Language language) {
		return Arrays.stream(values).map(t -> toOption(t, language)).collect(Collectors.toList());
	}

	static <T extends LabelI18nProxy<?, ?>> List<Option<String>> toMetas(T[] values, @Nonnull Predicate<? super T> predicate) {
		return toMetas(values, predicate, null);
	}

	static <T extends LabelI18nProxy<?, ?>> List<Option<String>> toMetas(T[] values, @Nonnull Predicate<? super T> predicate, @Nullable Language language) {
		return Arrays.stream(values).filter(predicate).map(t -> toOption(t, language)).collect(Collectors.toList());
	}

	static Option<String> toMeta(ValueProxyImpl<?, ?> value) {
		return new Option<>(StringUtil.toString(value.getValue()), value.getLabel());
	}

	static List<Option<String>> toMetas(ValueProxyImpl<?, ?>[] values, @Nullable Language language) {
		return Arrays.stream(values).map(t -> toOption(t, language)).collect(Collectors.toList());
	}

	static <T extends ValueProxyImpl<?, ?>> List<Option<String>> toMetas(T[] values, @Nonnull Predicate<? super T> predicate, @Nullable Language language) {
		return Arrays.stream(values).filter(predicate).map(t -> toOption(t, language)).collect(Collectors.toList());
	}

	static Option<String> toOption(ValueProxyImpl<?, ?> t, @Nullable Language language) {
		return new Option<>(StringUtil.toString(t.getValue()), language == null ? t.getLabel() : language.msg(t.getProxy().label));
	}

}
